<?php
class jsonObject implements Iterator{

    private $data = array();

    public function __construct($array){
        $this->data = $this->recursive_Replace($array);
    }
   
    public function json_encode()
    {
        return json_encode($this->recursive_Restore($this->data));
    }       
   
    private function __set($key, $value){
    	$this->data[$key] = is_array($value) ? $this->recursive_Replace($value) : $value;
    }
   
    private function __get($key){
        if(isset($this->data[$key])) return $this->data[$key];
        throw new Exception('Missing Key:'.$key);
    }
   
    private function recursive_Replace($o){
        foreach($o as $k => $v){
            if(is_array($o[$k])) $o[$k] = new jsonObject($v);
        }
        return $o;
    }   
 
    private function recursive_Restore($o){
        foreach($o as $k => $v){
            if(is_object($v)) $o[$k] = $this->recursive_Restore($v->data);
        }
        return $o;
    }

    public function rewind() {
        reset($this->data);
    }

    public function current() {
        return current($this->data);
    }

    public function key() {
        return key($this->data);
    }

    public function next() {
        return next($this->data);
    }

    public function valid() {
        return $this->current() !== false;
    }
}
?>